package org.dromara.system.utils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;
import java.nio.charset.StandardCharsets;

public class UrlParser {

    public static void main(String[] args) {
        String url = "https://cbbhk.com/Checkout/Fingate_Return.aspx?securityToken=a1f6c140847f4e9d901767c388a9af32&SystemOrderID=20250401155732220156&Status=Failed&UniqueID=&PaymentInfo=Internal+Server+Error";
        Map<String, String> params = parseQueryParams(url);
        params.forEach((key, value) -> System.out.println(key + ": " + value));
    }
    public static Map<String, String> parseQueryParams(String url) {
        Map<String, String> queryParams = new HashMap<>();
        int queryStartIndex = url.indexOf('?');
        if (queryStartIndex == -1) {
            return queryParams; // 无查询参数
        }
        String query = url.substring(queryStartIndex + 1);
        String[] pairs = query.split("&");
        for (String pair : pairs) {
            String[] keyValue = pair.split("=", 2); // 分割键和值
            String key = decode(keyValue[0]);
            String value = keyValue.length > 1 ? decode(keyValue[1]) : "";
            queryParams.put(key, value);
        }
        return queryParams;
    }
    private static String decode(String encoded) {
        try {
            return URLDecoder.decode(encoded, StandardCharsets.UTF_8.name());
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("UTF-8编码不支持", e);
        }
    }
}
